package com.myschool.server.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myschool.server.manager.UserManager;
import com.myschool.server.model.Json;
import com.myschool.server.model.Rows;
import com.myschool.server.model.User;
import com.myschool.server.model.UserType;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

@Controller 
public class ClassSubjectController {
	
	@Autowired
	UserManager userManager;
	
	// @Autowired
	  //  private MailSender mailSender;
	
	@RequestMapping("subjectDetails.htm")
	public @ResponseBody
	Json SubjectDetails(HttpServletRequest request,String sidx, String sord, int page, int rows, Boolean _search, String searchField, String searchOper, String searchString)
	{

		Json jsonData = new Json();
		try 
		{
			String subID =  request.getParameter("id");
	
		jsonData.page = page; 
		jsonData.total = 2;
		jsonData.record = 2;
		
		jsonData.rows = new Rows[2] ;
		int count  =1;
		jsonData.rows[0] = new   Rows(); 
		jsonData.rows[1] = new   Rows(); 
		if( Integer.valueOf(subID)  == 2)
		{
		for(Rows row : jsonData.rows)
		{ 
			row.i= count;
			row.cell =  new String[] {"1","Maths","Rosy Miss", "No comments!", };
			count++;
		}
		}
		else
		{
		for(Rows row : jsonData.rows)
		{ 
			row.i= count;
			row.cell =  new String[] {"1","Social","Komala Miss", "Super", };
			count++;
		}
		}
		}
		catch(Exception ex)
		{
			System.out.println(ex.getMessage());
			
		}

		return jsonData; 
	} 
	
	@RequestMapping(value=  "addSubject.htm", method = RequestMethod.POST  )
	public  void addSubject(HttpServletRequest request,HttpServletResponse response )
	{
		try
		{
			String subjetName =  request.getParameter("subject"); 
			String teacher =  request.getParameter("teacher"); 
			String comments =  request.getParameter("comments"); 
			System.out.println(subjetName);
			System.out.println(teacher);
	 		System.out.println(comments);
		}
		catch(Exception ex)
		{
			System.out.println(ex.getMessage());
			
		}
	 	 
	}
	
	
	@RequestMapping("teacherList.htm")

	HttpServletResponse GetTeachersList(HttpServletRequest request,HttpServletResponse response)
	{
		
		String teachersList = "";
		List<User> teachers= 	userManager.retrieveUserByType(UserType.TEACHER) ;
		
		for(User teacher : teachers)
		{
			String id = Long.toString(teacher.getEntityId());
			String teacherName =teacher.getFirstName();
			if(teachersList == "")
			teachersList =  id + ":" + teacherName;
			else
			teachersList = teachersList + ";" + id + ":" + teacherName;
			System.out.println(teachersList);
			
		}
		
		
	try {
			PrintWriter writer = response.getWriter();
			writer.append(teachersList);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	
	}
	
	@RequestMapping("subjectList.htm") 
	HttpServletResponse GetSubjectList(HttpServletRequest request,HttpServletResponse response)
	{
		
		String subjectsList = "";
	/*	List<User> teachers= 	userManager.retrieveUserByType(UserType.TEACHER) ;
		
		for(User teacher : teachers)
		{
			String id = Long.toString(teacher.getId());
			String teacherName =teacher.getFirstName();
			
			teachersList = teachersList + ";" + id + ":" + teacherName;
			System.out.println(teachersList);
			
		}*/
		
		
	try {
			PrintWriter writer = response.getWriter();
			writer.append("1:Maths;2:Science;3:English");
			writer.close(); 
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	
	} 
	
}
